# pylint: disable-next=wrong-spelling-in-comment
"""Build tests"""


def scons():
    """Execute build"""
    Import('env', 'prereqs')

    # ----------------------
    # Common build configs
    denv = env.Clone()
    denv.AppendUnique(RPATH_FULL=['$PREFIX/lib64/daos_srv'])
    # for ddb includes
    denv.AppendUnique(CPPPATH=[Dir('../').srcnode()])
    denv.AppendUnique(LIBPATH=[Dir('../')])
    # Add include directory for vos internal headers
    denv.AppendUnique(CPPPATH=[Dir('../../../vos/').srcnode()])
    denv.AppendUnique(CPPPATH=[Dir('../../../vos/')])

    # spdk libraries
    spdk_libs = ['rte_bus_pci', 'rte_eal', 'rte_kvargs', 'rte_mbuf', 'rte_mempool_ring',
                 'rte_mempool', 'rte_pci', 'rte_power', 'rte_ring', 'spdk_bdev_aio',
                 'spdk_bdev_nvme', 'spdk_bdev', 'spdk_bdev', 'spdk_blob_bdev', 'spdk_blob_bdev',
                 'spdk_blob', 'spdk_blob', 'spdk_env_dpdk', 'spdk_event_bdev', 'spdk_event',
                 'spdk_init', 'spdk_init', 'spdk_json', 'spdk_json', 'spdk_jsonrpc', 'spdk_log',
                 'spdk_log', 'spdk_log', 'spdk_notify', 'spdk_nvme', 'spdk_nvme', 'spdk_rpc',
                 'spdk_sock', 'spdk_thread', 'spdk_thread', 'spdk_trace', 'spdk_util', 'spdk_vmd']

    # ----------------------
    # Build integration test
    tenv = denv.Clone()
    tenv.AppendUnique(CPPDEFINES='_GNU_SOURCE')
    # Add runtime paths for daos libraries
    tenv.AppendUnique(RPATH_FULL=['$PREFIX/lib64'])
    prereqs.require(tenv, 'argobots', 'protobufc', 'pmdk', 'spdk')

    libs = ['vos', 'uuid', 'daos_common_pmem', 'abt', 'gurt', 'bio', 'cart', 'cmocka', 'ddb']
    src = ['ddb_cmd_options_tests.c', 'ddb_commands_print_tests.c', 'ddb_commands_tests.c',
           'ddb_main_tests.c', 'ddb_parse_tests.c', 'ddb_path_tests.c', 'ddb_test_driver.c',
           'ddb_vos_tests.c']
    ddb_tests = tenv.d_program('ddb_tests', src, LIBS=libs + spdk_libs)

    tenv.Install('$PREFIX/bin/', ddb_tests)

    # ---------------
    # Build unit test
    tenv = denv.Clone()
    tenv.AppendUnique(CPPPATH=[Dir('../../../bio/').srcnode()])
    tenv.Append(CPPDEFINES=['-DDAOS_PMEM_BUILD'])
    prereqs.require(tenv, 'argobots', 'spdk', 'pmdk')
    # Required for dtx_act_discard_invalid tests.
    # This function is validated by its respective unit tests.
    tenv.AppendUnique(LINKFLAGS=['-Wl,--wrap=vos_dtx_discard_invalid'])

    libs = ['uuid', 'daos_common_pmem', 'gurt', 'vea', 'abt', 'bio', 'cmocka', 'pthread', 'pmemobj']
    src = ['ddb_ut.c', 'ddb_vos_ut.c']
    # vos and mock object files to wrap vos symbols
    vos_obj = tenv.Object(Glob('../../../vos/*.c'))
    mock_obj = tenv.Object(Glob('../../../dtx/tests/*_mock.c'))
    mgmt_obj = tenv.Object('../../../mgmt/mgmt_common.c')
    # Pylint does not like the exclude keyword below, but it works.
    # pylint: disable=unexpected-keyword-arg
    ddb_obj = tenv.Object(Glob('../*.c', exclude=['../ddb_entry.c']))
    ddb_ut = tenv.d_program('ddb_ut', src + vos_obj + mock_obj + mgmt_obj + ddb_obj,
                            LIBS=libs + spdk_libs)

    tenv.Install('$PREFIX/bin/', ddb_ut)


if __name__ == "SCons.Script":
    scons()
